﻿@namespace LaserMark.Components
@using LaserMark.DataAccess.Service
@using LaserMark.ModbusTCP
@using LaserMark.ModbusTCP.Abstraction
@implements IDisposable

<div class="altimetry-parameters">
    <ValidateForm Model="@Model" @ref="ValidateForm">
        <GroupBox Title="测高仪参数" class="mt-3">
            <div class="row g-3 form-inline">
                <div class="col-6">
                    <label class="form-label">测高仪地址</label>
                    <BootstrapInputGroup>
                        <BootstrapInputGroupLabel DisplayText="http://"></BootstrapInputGroupLabel>
                        <IpAddress @bind-Value="Model.AltimetryAddress"></IpAddress>
                    </BootstrapInputGroup>
                </div>
                <div class="col-6">
                    <BootstrapInput @bind-Value="Model.AltimetryPort" ShowLabel="true" DisplayText="测高仪端口"></BootstrapInput>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.UltrasonicOffsetX" ShowLabel="true" DisplayText="测高仪 X 偏移(mm)"></BootstrapInputNumber>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.UltrasonicOffsetY" ShowLabel="true" DisplayText="测高仪 Y 偏移(mm)"></BootstrapInputNumber>
                </div>
                <div class="col-6">
                    <BootstrapInput @bind-Value="Model.MarkPerfectHeight" ShowLabel="true" DisplayText="打码最佳高度(mm)"></BootstrapInput>
                </div>
                <div class="col-6">
                    <BootstrapInput @bind-Value="Model.MarkMaxHeight" ShowLabel="true" DisplayText="打码机最高高度(mm)"></BootstrapInput>
                </div>
                <div class="col-6">
                    <BootstrapInput @bind-Value="Model.MarkMinHeight" ShowLabel="true" DisplayText="打码机最低高度(mm)"></BootstrapInput>
                </div>
                <div class="col-6">
                    <BootstrapInput @bind-Value="Model.MarkCoefficient" ShowLabel="true" DisplayText="比例系数" IsDisabled="true"></BootstrapInput>
                </div>
                <div class="col-6">
                    <BootstrapInput @bind-Value="Model.MarkMaxAD" ShowLabel="true" DisplayText="打码机最高电压" IsDisabled="true"></BootstrapInput>
                </div>
                <div class="col-6">
                    <BootstrapInput @bind-Value="Model.MarkMinAD" ShowLabel="true" DisplayText="打码机最低电压" IsDisabled="true"></BootstrapInput>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.HeightDiffer" ShowLabel="true" DisplayText="最佳打码高度差(mm)"></BootstrapInputNumber>
                </div>
            </div>
        </GroupBox>
    </ValidateForm>
</div>

<DialogFooter OnSave="@OnSave">
    <BodyTemplate>
        <Button Icon="fa-solid fa-calculator" Text="高电压" IsAsync="true" OnClick="async e => await OnCalcMaxAD()" IsDisabled="@IsMaxAd"></Button>
        <Button Icon="fa-solid fa-calculator" Text="低电压" IsAsync="true" OnClick="async e => await OnCalcMinAD()" IsDisabled="@IsMinAd"></Button>
        <Button Icon="fa-solid fa-calculator" Text="计算比例系数" OnClick="async e => await OnCalcCoefficient()"></Button>
    </BodyTemplate>
</DialogFooter>


@code {

    [NotNull]
    private ValidateForm? ValidateForm { get; set; }

    [Inject]
    [NotNull]
    private IMarkArgsService? MarkArgsService { get; set; }

    [Inject]
    [NotNull]
    private ISecretService? SecretService { get; set; }

    [Inject]
    [NotNull]
    private ToastService? ToastService { get; set; }

    [Inject]
    [NotNull]
    private IMasterClientFactory? MasterClientFactory { get; set; }

    private MarkArgs Model { get; set; } = new();

    [NotNull]
    private ModBusMaster? Master { get; set; }

    private bool IsMaxAd { get; set; }

    private bool IsMinAd { get; set; }


    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        Model = await MarkArgsService.GetMarks();

        Master = MasterClientFactory.Create();
        await Master.ConnectAsync();
    }

    private async Task<bool> OnSave(string password)
    {
        var ret = ValidateForm.Validate();
        var res = await SecretService.GetSecret("Customer", password);
        if (res)
        {
            await MarkArgsService.UpdateMarks(Model);
            return true;
        }
        else
        {
            return false;
        }
    }

    private async Task OnCalcMaxAD()
    {
        IsMaxAd = true;
        var arr = new List<double>();
        for (int i = 0; i < 10; i++)
        {
            var aa = await Master.RedKeepRegister(0, 1);
            arr.Add(aa);
        }

        Model.MarkMaxAD = arr.Average();
        IsMaxAd = false;
    }


    private async Task OnCalcMinAD()
    {
        IsMinAd = true;
        var arr = new List<double>();
        for (int i = 0; i < 10; i++)
        {
            var aa = await Master.RedKeepRegister(0, 1);
            arr.Add(aa);
        }

        Model.MarkMinAD = arr.Average();
        IsMinAd = false;
    }

    private Task OnCalcCoefficient()
    {
        Model.MarkCoefficient = (Model.MarkMinAD - Model.MarkMaxAD) / (Model.MarkMinHeight - Model.MarkMaxHeight);
        return Task.CompletedTask;
    }

    public void Dispose()
    {
        Master.Dispose();
        GC.SuppressFinalize(this);
    }
}
